package edu.hawaii.examschd.commands;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import edu.hawaii.examschd.myClass;
import edu.hawaii.examschd.modules.ExamScheduler;
import edu.hawaii.examschd.modules.Cal;

/**
 * Defines the get-cal command.
 * 
 */
public class GetCal implements Command {
  private String name, description, usage, input;
  private ExamScheduler client;
  private Cal cal;

  /**
   * Creates an instance of this command with the client set to null by default, requiring
   * initialization.
   */
  public GetCal() {
    this.name = "get-cal";
    this.usage = "get-cal [crn]";
    this.description =
        "returns a calendar containing the exam time for a class with the specified CRN.";
  }

  /**
   * Initializes this command with a healthy ExamScheduler client.
   * 
   * @param client A healthy client used to initialize the command
   * @param mod Module instance that is used for this command.
   * @param args Arguments required by the command for execution
   * @return Whether or not the arguments passed was accepted by the command initialization
   */
  @Override
  public boolean initializeCommand(ExamScheduler client, String[] args) {
    this.client = client;
    if (args.length != 2) {
      System.out.format("The arguments are invalid " + "(Enter \"help\" to see command usage");
      return false;
    }
    this.input = args[1];
    cal = new Cal();
    return true;
  }

  /**
   * Returns the name of this command.
   * 
   * @return the name of this command
   */
  @Override
  public String getName() {
    return this.name;
  }

  /**
   * Returns the usage syntax of this command.
   * 
   * @return the usage syntax of this command
   */
  @Override
  public String getUsage() {
    return this.usage;
  }

  /**
   * Returns the description of this command.
   * 
   * @return a description of this command
   */
  @Override
  public String getDescription() {
    return this.description;
  }

  /**
   * Initialize the SourceData with the power data obtained from the client.
   * 
   * @param name The name of the tower or lounge to get data from
   * @return Whether or not the data was successfully initialized
   */
  public boolean initializeData(String name) {
    return true;
  }

  /**
   * Prints the results of executing this command.
   * 
   */
  @Override
  public void printResults() {
    try {
      File file = new File("exam.ics");
      if (!file.exists()) {
        file.createNewFile();
        System.out.println("New file exam.ics has been creates in the current directory.");
      }
      else {
        System.out.println("Exam has been appended to exam.ics");
      }
      FileWriter fw = new FileWriter("exam.ics", true);
      Integer crn = Integer.parseInt(this.input);
      String exam = this.client.get_by_crn_as_str(crn);
      myClass temp = this.client.get_all_by_crn(crn);
      String output = cal.createICS(temp) + "\r\n";
      fw.write(output);
      fw.close();

    }
    catch (FileNotFoundException fnfe) {
      System.out.println("File not Found!");
    }
    catch (IOException ioe) {
      System.out.println("IOE");
    }

  }

  // public static void main(String[] args) {
  // CurrentPower cp = new CurrentPower();
  // String[] arg = { "current-power", "Ilima-A" };
  // WattDepotClient cl = new WattDepotClient(args[0]);
  // if (cp.initializeCommand(cl, arg)) {
  // if (cp.initializeData(arg[1])) {
  // cp.printResults();
  // }
  // }

  // }
}